﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ProgramMain.Database;
using ProgramMain.Database.SimpleOpticNetDBTableAdapters;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class CableList : ProgramMain.Framework.BaseGridListCtl
    {
        public CableList()
        {
            InitializeComponent();
        }

        protected override bool IsLoadDelayed()
        {
            return !DetailID.HasValue || Present == PresentFormType.InCables;
        }

        protected override void LoadData()
        {
            //прочитать данные из базы и заполнить датасет
            this.cableTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            switch (Present)
            {
                case PresentFormType.InVertexes:
                    if (DetailID.HasValue)
                        this.cableTableAdapter.Fill(this.simpleOpticNetDB.Cable, null, DetailID, null);
                    else this.simpleOpticNetDB.Cable.Clear();
                    break;
                case PresentFormType.InCableTunnels:
                    if (DetailID.HasValue)
                        this.cableTableAdapter.Fill(this.simpleOpticNetDB.Cable, DetailID, null, null);
                    else this.simpleOpticNetDB.Cable.Clear();
                    break;
                case PresentFormType.InCables:
                    this.cableTableAdapter.Fill(this.simpleOpticNetDB.Cable, null, null, DetailID);
                    break;
                default:
                    this.simpleOpticNetDB.Cable.Clear();
                    break;
            }

            UpdateStatusText(this.simpleOpticNetDB.Cable);
        }

        protected override void UpdateData()
        {
            //внести изменения в базу
            this.cableTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.cableTableAdapter.Update(this.simpleOpticNetDB.Cable);
            UpdateStatusText(this.simpleOpticNetDB.Cable);
        }

        override protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            return new CableDetail();
        }

        private void CableList_DetailIDChanged(object sender, DetailEventArgs e)
        {
            DBRead();
        }

        protected override void biDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (Present)
            {
                case PresentFormType.InVertexes:
                    if (Framework.BaseSecurity.CheckSecurityViewAction())
                    {
                        FrmCables frmCables = FrmCables.ShowForm(false);
                        frmCables.SetActiveCable(ViewRowID.Value);
                    }
                    break;
                case PresentFormType.InCableTunnels:
                    if (Framework.BaseSecurity.CheckSecurityViewAction())
                    {
                        FrmCables frmCables = FrmCables.ShowForm();
                        if (dataSource.Current != null)
                        {
                            SimpleOpticNetDB.CableRow row =
                                (SimpleOpticNetDB.CableRow)((DataRowView)dataSource.Current).Row;
                            frmCables.SetActiveCable(row.ID);
                        }
                    }
                    break;
                case PresentFormType.InCables:
                     if (Framework.BaseSecurity.CheckSecurityViewAction())
                    {
                       
                       if (dataSource.Current != null)
                       {
                           SimpleOpticNetDB.CableRow row =
                               (SimpleOpticNetDB.CableRow) ((DataRowView) dataSource.Current).Row;
                           FrmVertexes frmVertexes = FrmVertexes.ShowForm(false);
                           frmVertexes.SetCable(row.ID);
                       }
                    }
                    break;
            }
        }

        public SimpleOpticNetDB.CableRow AddCable()
        {
            if (!CheckSecurityEditAction()) return null;
            CableDetail dialog = new CableDetail();
            dialog.dataSource = dataSource;
            dialog.dataSource.AddNew();

            SimpleOpticNetDB.CableRow row = (SimpleOpticNetDB.CableRow)((DataRowView)dataSource.Current).Row;
            if (row != null)
            {
                DialogResult dlgRes = dialog.ShowModal(BaseModalDetailCtl.ShowModeTypes.Add);
                if (dlgRes == DialogResult.OK)
                {
                    this.cableTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
                    this.cableTableAdapter.Update(row);
                    return row;
                }
            }
            return null;
        }

        protected override void biPathMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           if(dataSource.Current == null) return;
           SimpleOpticNetDB.CableRow row = (SimpleOpticNetDB.CableRow)((DataRowView)dataSource.Current).Row;

           FrmOpticMap map = FrmOpticMap.ShowForm();
           map.ClearLighted();
           VertexesTableAdapter vertexesTableAdapter = new VertexesTableAdapter();
           vertexesTableAdapter.ClearBeforeFill = true;
           vertexesTableAdapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;
           SimpleOpticNetDB.VertexesDataTable vertexesDataTable = vertexesTableAdapter.GetData(null, row.ID, null, null,
                                                                                              null, null, null, false, null);
           try
           {
               SimpleOpticNetDB.VertexesRow v =
                   vertexesDataTable.Cast<SimpleOpticNetDB.VertexesRow>().Cast<SimpleOpticNetDB.VertexesRow>().First();
                  
               map.SetCenterMapObject(v.Longitude, v.Latitude);
               foreach (SimpleOpticNetDB.VertexesRow item in vertexesDataTable.Cast<SimpleOpticNetDB.VertexesRow>())
               {
                   map.AddVertesLighted(item.ID);
               }
           }
           catch (Exception)
           {
           }

           CableTunnelsTableAdapter cableTunnelsTableAdapter = new CableTunnelsTableAdapter();
           cableTunnelsTableAdapter.ClearBeforeFill = true;
           cableTunnelsTableAdapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;
           SimpleOpticNetDB.CableTunnelsDataTable cableTunnelsDataTable = cableTunnelsTableAdapter.GetData(null, null, null, null, null, null, null, row.ID, null);

           foreach (SimpleOpticNetDB.CableTunnelsRow item in cableTunnelsDataTable)
           {
               map.AddCableTunnelLighted(item.ID);
           }
        }

        protected override void biAddDocument_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current != null)
            {
                SimpleOpticNetDB.CableRow row = (SimpleOpticNetDB.CableRow)((DataRowView)dataSource.Current).Row;
                new DocumentList() { CurveElementID = row.ID }.AddDocument();

            }
        }

        protected override void biDocumentList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current != null)
            {
                SimpleOpticNetDB.CableRow row = (SimpleOpticNetDB.CableRow)((DataRowView)dataSource.Current).Row;
                new FrmDocumentList() { CurveElementID = row.ID }.ShowDialog();
            }
        }
    }
}
